<?php

namespace app\modules\models;

use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use Yii;
use yii\helpers\ArrayHelper;
use yii\behaviors\BlameableBehavior;

class Order extends ActiveRecord
{

//    const AK = 'tfmqlymL0sk_6I64Yr1bPAQsjxY9TDp1izav8hOr';
//    const SK = 'jI3u8GmFp7WYpXRC5-P1xmgmJlH6g7gQPIHUuboU';
//    const DOMAIN = 'p3g838d43.bkt.clouddn.com';
//
//    const BUCKET = 'imooc-shop';

    public $statusname;
    public $adminname;
    public function behaviors()
    {
        return [
            [
                'class' => BlameableBehavior::className(),
                'createdByAttribute' => 'cuid',
                'updatedByAttribute' => 'uuid',
                'value' => Yii::$app->admin->id,
            ],
            [
                'class'              => TimestampBehavior::className(),
                'createdAtAttribute' => 'createTime',
                'updatedAtAttribute' => 'updateTime',
                'value'              => time(),
            ],
        ];
    }

    public static function tableName()
    {
        return "{{%order}}";
    }

    public function attributeLabels()
    {
        return [
            'productid' => '关联产品',
            'partsid'=>'产品零件',
            'contractNum'=>'合同编号',
            'question'=>'报修问题',
            'info'=>'订单详情',
            'adminid'=>'指派师傅',
            'coverUrl'=>'维修图片',
        ];
    }

    public function rules()
    {
        return [
            ['productid', 'required', 'message' => '产品名称不能为空','on'=>['add','update']],
            ['partsid', 'required', 'message' => '产品零件不能为空','on'=>['add','update']],
            ['contractNum', 'required', 'message' => '合同编号不能为空','on'=>['add','update']],
            ['question', 'required', 'message' => '订单问题不能为空','on'=>['add','update']],
            ['info', 'required', 'message' => '订单详情不能为空','on'=>['add','update']],
            [['coverUrl'], 'required','on'=>['coverUrl']],
        ];
    }

    public function add($data)
    {
        $this->scenario="add";
        $this->adminid=$data['Order']['adminid'];
        if ($this->load($data) && $this->save()) {
            return true;
        }
        return false;
    }

    public function coveradd($data)
    {
        $this->scenario='coverUrl';
        if ($this->load($data) && $this->save()) {
            return true;
        }
        return false;
    }


    public function updateDate($data,$id)
    {
        $this->scenario="update";
        if ($this->load($data)) {
            $this->updateAll($data['Order'], 'id = :id', [':id' => $id]);
            return true;
        }
        return false;
    }


    //查询单条数据对象
    public function getOne($id)
    {
        $data = self::find()->where('id = :id', [':id' => $id])->one();
        return $data;
    }

}





